﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Distribution.Controllers {
    
    public class ManageController : GeneralController
    {
        private IOrderService _iOrderService;
        private IFeatureService _iFeatureService;
        private IRoleService _iRoleService;
        private IManageService _iManageService;
        private IMerchantService _iMerchantService;

        public ManageController(IFeatureService iFeature, IRoleService roleService, IManageService iManageService, IMerchantService merchantService)
        {
            _iFeatureService = iFeature;
            _iRoleService = roleService;
            _iManageService = iManageService;
            _iMerchantService = merchantService;
        }
        public JsonResult GetMarchant(MerchantQuery marchantQuery)
        {
          //  marchantQuery.disabled = true;
            var list = _iMerchantService.GetMerchant(marchantQuery);
            return Json(ResultToJson.Json(list), JsonRequestBehavior.AllowGet);
        }
        #region 功能菜单表
        public JsonResult GetFeature(long id)
        {
            var result = _iFeatureService.Get(id);
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetFeatures(FeatureType id)
        {
            var features = _iFeatureService.Get(id);
            return Json(ResultToJson.Json(features), JsonRequestBehavior.AllowGet);
        }
        [HttpPut]
        public JsonResult PutFeature(feature_dto entity)
        {
            var result = _iFeatureService.Update(entity);
            return Json(ResultToJson.Json(result > 0 ? "保存成功" : "没有修改任何内容", result), JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult PostFeature(feature_dto entity)
        {
            var result = _iFeatureService.Add(entity);
            return Json(ResultToJson.Json(result > 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        [HttpDelete]
        public JsonResult DeleteFeature(long id)
        {
            var result = _iFeatureService.Delete(id);
            return Json(ResultToJson.Json(result > 0 ? "删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 角色
        public JsonResult GetRole(long id)
        {
            var result = _iRoleService.Get(id);
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetRoles()
        {
            var Roles = _iRoleService.GetAll(null);
            return Json(ResultToJson.Json(Roles), JsonRequestBehavior.AllowGet);
        }
        [HttpPut]
        [ValidateInput(false)]
        public JsonResult PutRole(long id)
        {
            var dto = _iRoleService.Get(id);
            TryUpdateModel(dto);
            var result = _iRoleService.Update(dto);
            return Json(ResultToJson.Json(result > 0 ? "保存成功" : "没有修改任何内容", result), JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult PostRole(role_dto dto)
        {
            var result = _iRoleService.Add(dto);
            return Json(ResultToJson.Json(result > 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        [HttpDelete]
        public JsonResult DeleteRole(long id)
        {
            var result = _iRoleService.Delete(id);
            return Json(ResultToJson.Json(result > 0 ? "删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 管理员
        public JsonResult GetManage(long id)
        {
            var result = _iManageService.Get(id);
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetManages()
        {
            var Manages = _iManageService.Get();
            return Json(ResultToJson.Json(Manages), JsonRequestBehavior.AllowGet);
        }
        [HttpPut]
        [ValidateInput(false)]
        public JsonResult PutManage(long id)
        {
            var dto = _iManageService.Get(id);
            TryUpdateModel(dto);
            var result = _iManageService.Update(dto);
            return Json(ResultToJson.Json(result > 0 ? "保存成功" : "没有修改任何内容", result), JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult PostManage(manage_dto dto)
        {
            var result = _iManageService.Add(dto);
            return Json(ResultToJson.Json(result > 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        [HttpDelete]
        public JsonResult DeleteManage(long id)
        {
            var result = _iManageService.Delete(id);
            return Json(ResultToJson.Json(result > 0 ? "删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}